﻿using System;
using System.Xml.Linq;
using System.Collections.Generic;

using Game;
using Engine;
namespace ApiToolkit
{
	public class FoodTypePropertyEditor : CanvasWidget
	{
		PropertyEditor_Common Editor;
		ContainerWidget Parent;
		
		FoodType value;
		ButtonWidget ValueButton;
		ButtonWidget ApplyButton;
		public FoodTypePropertyEditor(int content, string targetProperty, ContainerWidget parent)
		{
			Parent = parent;
			
			Editor = new PropertyEditor_Common(content, targetProperty);

			XElement xelement = ContentManager.Get<XElement>("Widgets/FoodTypePropertyEditor");

			LoadContents(this, xelement);

			ValueButton = Children.Find<ButtonWidget>("ValueButton");
			ApplyButton = Children.Find<ButtonWidget>("ApplyButton");
			
			
			value = (FoodType)Editor.TargetPropertyValue;
			ValueButton.Text = value.ToString();
		}
		public override void Update()
		{
			if(ValueButton.IsClicked)
			{
				ValueButtonClicked();
			}
			if(ApplyButton.IsClicked)
			{
				Editor.TargetPropertyValue = value;
			}
		}
		public void ValueButtonClicked()
		{
			var list = new List<FoodType>(){ FoodType.None, FoodType.Fish, FoodType.Meat, FoodType.Bread, FoodType.Fruit, FoodType.Grass};
			
			Func<object, Widget> fact = (item) =>
			{
				return new LabelWidget()
				{
					Text = item.ToString(),
					HorizontalAlignment = WidgetAlignment.Center
				};
			};
			
			Action<object> onClose = (selectedItem) =>
			{
				ValueButton.Text = selectedItem.ToString();
				value = (FoodType)selectedItem;
			};
			
			var selection = new ListSelectionDialog("选择分类", list, 38, fact, onClose);
			
			DialogsManager.ShowDialog(Parent, selection);

		}
	}
}